package com.yxzz.core.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.yxzz.core.domain.PDiagnosisRecord;
import com.yxzz.core.domain.PDiagnosisRelationship;
import com.yxzz.core.mapper.PDiagnosisRecordMapper;
import com.yxzz.core.service.IPDiagnosisRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 中医诊断记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-28
 */
@Service
public class PDiagnosisRecordServiceImpl implements IPDiagnosisRecordService 
{
    @Autowired
    private PDiagnosisRecordMapper pDiagnosisRecordMapper;

    /**
     * 查询中医诊断记录
     * 
     * @param id 中医诊断记录主键
     * @return 中医诊断记录
     */
    @Override
    public PDiagnosisRecord selectPDiagnosisRecordById(Long id)
    {
        return pDiagnosisRecordMapper.selectPDiagnosisRecordById(id);
    }

    /**
     * 查询中医诊断记录列表
     * 
     * @param pDiagnosisRecord 中医诊断记录
     * @return 中医诊断记录
     */
    @Override
    public List<PDiagnosisRecord> selectPDiagnosisRecordList(PDiagnosisRecord pDiagnosisRecord)
    {
        return pDiagnosisRecordMapper.selectPDiagnosisRecordList(pDiagnosisRecord);
    }

    /**
     * 新增中医诊断记录
     * 
     * @param pDiagnosisRecord 中医诊断记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPDiagnosisRecord(PDiagnosisRecord pDiagnosisRecord)
    {
        int rows = pDiagnosisRecordMapper.insertPDiagnosisRecord(pDiagnosisRecord);
        insertPDiagnosisRelationship(pDiagnosisRecord);
        return rows;
    }

    /**
     * 修改中医诊断记录
     * 
     * @param pDiagnosisRecord 中医诊断记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePDiagnosisRecord(PDiagnosisRecord pDiagnosisRecord)
    {
        pDiagnosisRecordMapper.deletePDiagnosisRelationshipByDiagnosisId(pDiagnosisRecord.getId());
        insertPDiagnosisRelationship(pDiagnosisRecord);
        return pDiagnosisRecordMapper.updatePDiagnosisRecord(pDiagnosisRecord);
    }

    /**
     * 批量删除中医诊断记录
     * 
     * @param ids 需要删除的中医诊断记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePDiagnosisRecordByIds(Long[] ids)
    {
        pDiagnosisRecordMapper.deletePDiagnosisRelationshipByDiagnosisIds(ids);
        return pDiagnosisRecordMapper.deletePDiagnosisRecordByIds(ids);
    }

    /**
     * 删除中医诊断记录信息
     * 
     * @param id 中医诊断记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePDiagnosisRecordById(Long id)
    {
        pDiagnosisRecordMapper.deletePDiagnosisRelationshipByDiagnosisId(id);
        return pDiagnosisRecordMapper.deletePDiagnosisRecordById(id);
    }

    /**
     * 新增诊断记录与治疗药方等的关系信息
     * 
     * @param pDiagnosisRecord 中医诊断记录对象
     */
    public void insertPDiagnosisRelationship(PDiagnosisRecord pDiagnosisRecord)
    {
        List<PDiagnosisRelationship> pDiagnosisRelationshipList = pDiagnosisRecord.getPDiagnosisRelationshipList();
        Long id = pDiagnosisRecord.getId();
        if (StringUtils.isNotNull(pDiagnosisRelationshipList))
        {
            List<PDiagnosisRelationship> list = new ArrayList<PDiagnosisRelationship>();
            for (PDiagnosisRelationship pDiagnosisRelationship : pDiagnosisRelationshipList)
            {
                pDiagnosisRelationship.setDiagnosisId(id);
                list.add(pDiagnosisRelationship);
            }
            if (list.size() > 0)
            {
                pDiagnosisRecordMapper.batchPDiagnosisRelationship(list);
            }
        }
    }
}
